import java.io.IOException;
import java.net.Socket;

public class SocketServer extends Thread {
    private Socket clientSocket;
    private static final LoadBalancer loadBalancer = LoadBalancer.getInstance();

    public SocketServer(Socket socket) {
        this.clientSocket = socket;
        setDaemon(true);
        setPriority(NORM_PRIORITY);
        start();
    }

    public void run() {
        try {
            String query = Serialization.readObject(clientSocket).toString();
            String result = loadBalancer.execute(query);
            Serialization.writeObject(clientSocket,result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
